#####################################################################
#####################################################################
##								   ##
##		+++++				+++++		   ##
## 		--------------<AntiPvP>--------------		   ##
##		+++++				+++++		   ##
##								   ##
##		<<<<<<<<<<<<<<--------->>>>>>>>>>>>>>		   ##
##	<<<<<<<<				     >>>>>>>>	   ##
##  <<<<<<       Project AntiPvP by SOADfan and Team       >>>>>>  ##
##	<<<<<<<<				     >>>>>>>>	   ##
##		<<<<<<<<<<<<<<--------->>>>>>>>>>>>>>		   ##
##								   ##
##								   ##
##	>v0.0.1 First idea only lvl punishment worked.		   ##
##								   ##
##	>v0.1.0	Added a so called "square check" which		   ##
##		punishes all players who PvP in it. 		   ##
##		NPC's don't get punished for killing 		   ##
##		players anymore.^^				   ##
##								   ##
##	>v0.2.5	Added 1 "Warning" for players...		   ##
##		If they still ignore the rules 30 		   ##
##		minutes of jail awaits them :P			   ##
##		Requires ngJail v1.1.9				   ##
##								   ##
##	>v0.3.5 Added a "ZoneCheck" so whole zones can		   ##
##		be checked for unallowed PvP.			   ##
##								   ##
##	>v0.3.6 Added command to turn AntiPvP on/off		   ##
##								   ##
##	>v0.4.0	Fixed a problem with jail. The time		   ##
##		to be in jail got replaced. Now it		   ##
##		gets stacked :P					   ##
##								   ##
##	>v0.5.5 Added more commands. GM's are now		   ##
##		allowed to set the jailtime and jail		   ##
##		can be disabled if necessary...			   ##
##		Allowed lvl difference can be set with		   ##
##		a command now. Added a command to list		   ##
##		all AntiPvP commands! (.antipvp)		   ##
##								   ##
##		There is a command for players too.		   ##
##		(.antipvp info)					   ##
##		Players can check the allowed lvl		   ##
##		difference, PvP forbidden places, the		   ##
##		jailtime they will receive and if		   ##
##		AntiPvP is active.				   ##
##								   ##
##	>v1.0.0 (Final version?) i have rewritten the		   ##
##		whole thing. Finally it's faster...		   ##
##		Added new commands for the admin, he can	   ##
##		create the checked areas now with ingame	   ##
##		commands :). So he doesnt have to change	   ##
##		the values in the arrays anymore. The		   ##
##		values are stored in text files in 		   ##
##		WoWEmu/AntiPvP/$filename.txt... There is	   ##
##		also a BackUp folder so the last area/zone	   ##
##		can be reset :). I wrote this tcl just for	   ##
##		those fucking noobs who dont stop to PvP	   ##
##		in forbidden zones and the livetest showed	   ##
##		that the number of rulebreaking PvP		   ##
##		reduced a lot. So Enjoy :)			   ##
##								   ##
##	>v2.0.0 Final version! (as long as i dont get other	   ##
##		ideas^^. There is finally a config to		   ##
##		change things :)! You can disable single	   ##
##		checks of AntiPvP for example			   ##
##		Level/Area/Zone checks. You can also decide	   ##
##		if all needed files for antipvp should be	   ##
##		autogenerated on startup(but u still have to 	   ##
##		make a folder called "AntiPvP" in your main	   ##
##		wowemu directory*). Config goes in		   ##
##		------>	    WoWEmu/scripts/conf/     <------	   ##
##		But you can create all missing files with	   ##
##		a command too. In this case disable the check	   ##
##		on startup of wowemu in the config.		   ##
##		Another thing added is the command		   ##
##		.antipvp event <minutes> <yards>		   ##
##		so you dont have to disable AntiPvP during 	   ##
##		events. People in this area dont get checked	   ##
##		for any requirements and dont get punished :)	   ##
##								   ##
##	>v2.1.0 The "Now i know how to create folders!"		   ##
##		version. All required folders/files are now	   ##
##		autogenerated by the tcl itself! :)		   ##
##								   ##
##								   ##
##					    (c) SOADfan and Team   ##
##								   ##
##								   ##
#####################################################################
#####################################################################


variable mapid
variable zoneid
variable atc
variable ztc
variable xtop
variable yleft
variable yright
variable xbottom
variable saytext
variable sayzone
variable EventOn 0
variable EventPos
variable EventArea
variable EventTime
variable EventStart

variable PvPJail 1
variable LvLCheck 1
variable AreaCheck 1
variable PvPActive 1
variable ZoneCheck 1
variable LoadCoords 1
variable LoadConf 1

variable maxleveldiff 10
variable PvPJailTime 1800

namespace eval ::AntiPvP {

proc ::AntiPvP::init { } {

    variable conf "scripts/conf/antipvp.conf"

	::AntiPvP::readConf $conf
}

proc ::AntiPvP::readConf { config } {

    global PvPJail
    global LvLCheck
    global AreaCheck
    global PvPActive
    global ZoneCheck
    global maxleveldiff
    global PvPJailTime
    global LoadConf

      if { $LoadConf == 1 } {

	foreach { section data } [ ::Custom::ReadConf $config ] {
		if { [ string tolower $section ] == "antipvp" } {
			foreach { key value } $data {
				set key [ string tolower $key ]

				switch $key {
					"antipvpactive" {
						set PvPActive $value
					}
					"jailactive" {
						set PvPJail $value
					}
					"allowedleveldiff" {
						set maxleveldiff $value
					}
					"jailtime" {
						set PvPJailTime $value
					}
					"createfilesonload" {
						set CreateOnLoad $value
					}
					"zonecheck" {
						set ZoneCheck $value
					}
					"levelcheck" {
						set LvLCheck $value
					}
					"areacheck" {
						set AreaCheck $value
					}
				}
			}
		}
	}

      } else {
	puts "The AntiPvP Config is disabled!"
      }

	if { $CreateOnLoad == 1 } {

		if { ![file exists "AntiPvP"] } {
		file mkdir "AntiPvP"
		}
		if { ![file exists "AntiPvP/BackUp"] } {
		file mkdir "AntiPvP/BackUp"
		}
		if { ![file exists "AntiPvP/map.txt"] } {
		set handle [open "AntiPvP/map.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/namearea.txt"] } {
		set handle [open "AntiPvP/namearea.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/namezone.txt"] } {
		set handle [open "AntiPvP/namezone.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/numberofareas.txt"] } {
		set handle [open "AntiPvP/numberofareas.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/numberofzones.txt"] } {
		set handle [open "AntiPvP/numberofzones.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/xcoordsbig.txt"] } {
		set handle [open "AntiPvP/xcoordsbig.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/xcoordssmall.txt"] } {
		set handle [open "AntiPvP/xcoordssmall.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/ycoordsbig.txt"] } {
		set handle [open "AntiPvP/ycoordsbig.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/ycoordssmall.txt"] } {
		set handle [open "AntiPvP/ycoordssmall.txt" w+]
		puts $handle
		close $handle
		}
		if { ![file exists "AntiPvP/zone.txt"] } {
		set handle [open "AntiPvP/zone.txt" w+]
		puts $handle
		close $handle
		}
	}
}

proc ::AntiPvP::nAntiPvP { player cargs } {

    global PvPJail
    global EventOn
    global saytext
    global sayzone
    global LvLCheck
    global EventPos
    global AreaCheck
    global PvPActive
    global EventArea
    global EventTime
    global EventStart
    global LoadCoords
    global ZoneCheck
    global maxleveldiff
    global PvPJailTime

     if { [ lindex $cargs 0 ] == "info" } {

	if { $PvPActive == 1 } {
	set PvPreturn ON
	} else {
	set PvPreturn OFF
	}

	if { $ZoneCheck == 1 } {
	set Zonereturn ON
	} else {
	set Zonereturn OFF
	}

	if { $LvLCheck == 1 } {
	set LvLreturn ON
	} else {
	set LvLreturn OFF
	}

	if { $AreaCheck == 1 } {
	set Areareturn ON
	} else {
	set Areareturn OFF
	}

	if { $LoadCoords == 1 } {
	   set rhandle [open "AntiPvP/namearea.txt" ]
	   gets $rhandle saytext
	   close $rhandle

	   set rhandle [open "AntiPvP/namezone.txt" ]
	   gets $rhandle sayzone
	   close $rhandle
	}

	set JailMins [ expr ($PvPJailTime /60)]
	return "You will receive one warning! After this you will be jailed!\nJailtime: $JailMins minutes\nAllowed level difference: +/-$maxleveldiff\nPvP restricted Areas: $saytext.\nPvP restricted Zones: $sayzone.\nZone check is $Zonereturn!\nArea check is $Areareturn!\nLeveL check is $LvLreturn!\nAntiPvP is $PvPreturn!"

     }

     if { [GetPlevel $player] >= 4 } {

	switch [ lindex $cargs 0 ] {

		"db" {
			if { ![file exists "AntiPvP"] } {
			file mkdir "AntiPvP"
			}
			if { ![file exists "AntiPvP/BackUp"] } {
			file mkdir "AntiPvP/BackUp"
			}
			if { [file exists "AntiPvP/map.txt"] && [file exists "AntiPvP/namearea.txt"] && [file exists "AntiPvP/namezone.txt"] && [file exists "AntiPvP/numberofareas.txt"] && [file exists "AntiPvP/numberofzones.txt"] && [file exists "AntiPvP/xcoordsbig.txt"] && [file exists "AntiPvP/xcoordssmall.txt"] && [file exists "AntiPvP/ycoordsbig.txt"] && [file exists "AntiPvP/ycoordssmall.txt"] && [file exists "AntiPvP/zone.txt"] } {
			return "The Database already exists!"
			} else {
			if { ![file exists "AntiPvP/map.txt"] } {
			set handle [open "AntiPvP/map.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/namearea.txt"] } {
			set handle [open "AntiPvP/namearea.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/namezone.txt"] } {
			set handle [open "AntiPvP/namezone.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/numberofareas.txt"] } {
			set handle [open "AntiPvP/numberofareas.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/numberofzones.txt"] } {
			set handle [open "AntiPvP/numberofzones.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/xcoordsbig.txt"] } {
			set handle [open "AntiPvP/xcoordsbig.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/xcoordssmall.txt"] } {
			set handle [open "AntiPvP/xcoordssmall.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/ycoordsbig.txt"] } {
			set handle [open "AntiPvP/ycoordsbig.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/ycoordssmall.txt"] } {
			set handle [open "AntiPvP/ycoordssmall.txt" w+]
			puts $handle
			close $handle
			}
			if { ![file exists "AntiPvP/zone.txt"] } {
			set handle [open "AntiPvP/zone.txt" w+]
			puts $handle
			close $handle
			}
			return "Missing files for the DB added!"
			}
		       }

		"off" {
			set PvPActive 0
			return "Anti-PvP is now deactivated!"
		      }

		"on" {
			set PvPActive 1
			return "Anti-PvP is now activated!"
		     }

		"zonecheck" {

			     switch [lindex $cargs 1] {

			     "off" {
				    set ZoneCheck 0
				    return "Zone check is now disabled!"
				   }

			     "on" {
				   set ZoneCheck 1
				   return "Zone check is now enabled!"
				  }
			     }

			     }

		"levelcheck" {

			      switch [lindex $cargs 1] {
 
			      "off" {
				     set LvLCheck 0
				     return "Level check is now disabled!"
				    }

			      "on" {
				    set LvLCheck 1
				    return "Level check is now enabled!"
				   }
			      }

			      }

		"areacheck" {

			      switch [lindex $cargs 1] {
 
			      "off" {
				     set AreaCheck 0
				     return "Area check is now disabled!"
				    }

			      "on" {
				    set AreaCheck 1
				    return "Area check is now enabled!"
				   }
			      }

			      }

		"jail" {

			switch [ lindex $cargs 1 ] {

			"off" {
			       set PvPJail 0
			       return "AntiPvP jailing is now disabled!"
			      }

			"on" {
			      set PvPJail 1
			      return "AntiPvP jailing is now enabled!"
			     }
			}

			}

		"jailtime" {
			    set PvPJailTime [ lindex $cargs 1 ]
			    set JailMins [ expr ($PvPJailTime /60)]
			    return "Anti-PvP jailtime is changed to $JailMins!"
			   }

		"leveldiff" {
			     set maxleveldiff [ lindex $cargs 1 ]
			     return "Anti-PvP max. level difference is now +/-$maxleveldiff!"
			    }

		"event" {
			 if { [ lindex $cargs 1 ] != "" } {
			 if { [ lindex $cargs 2 ] != "" } {
			 set EventOn 1
			 set EventStart [ clock seconds ]
			 set EventArea [ lindex $cargs 2 ]
			 set EventTime [ expr ([ lindex $cargs 1 ] * 60)]
			 set EventPos [lindex [GetPos $player]]
			 return "Event running for [lindex $cargs 1] minutes!"
			 } else {
			 set EventArea 75
			 }
			 }
			}

		 "" {
		     return "Commands: \n.antipvp db\n.antipvp info\n.antipvp on/off\n.antipvp jail on/off\n.antipvp zonecheck on/off\n.antipvp areacheck on/off\n.antipvp levelcheck on/off\n.antipvp leveldiff <level>\n.antipvp jailtime <seconds>\n.antipvp event <minutes> <yards>\n\nPlevel 6:\n.antipvp resetzone\n.antipvp resetarea\n.antipvp areacleanup\n.antipvp zonecleanup\n.antipvp addzone <zoneid>\n.antipvp delzone <number in list>\n.antipvp delarea <number in list>\n.antipvp namezone <name of the zone>\n.antipvp namearea <name of the area>\n.antipvp resetnamezone\n.antipvp resetnamearea\n.antipvp addarea <map> <big x coord> <small x coord> <big y coord> <small y coord>"
		    }
	    }

     }

     if { [GetPlevel $player] >= 6 } {

	switch [ lindex $cargs 0 ] {

		"namearea" {

			    set LoadCoords 1
			    set Characters [string length $cargs]
			    set NameArea [string range $cargs 9 $Characters]

			    set handle [open "AntiPvP/namearea.txt" ]
			    gets $handle line
			    close $handle

			    set handle [open "AntiPvP/BackUp/namearea.bak" w+]
			    puts $handle $line
			    close $handle

			    set handle [open "AntiPvP/namearea.txt" w+]
			    puts $handle "$line{$NameArea} "
			    close $handle

			    return "PvP is forbidden in $NameArea!"

			   }

		"resetnamearea" {

				 set LoadCoords 1

				 set handle [open "AntiPvP/BackUp/namearea.bak" ]
				 gets $handle line
				 close $handle

				 set handle [open "AntiPvP/namearea.txt" w+]
				 puts $handle $line
				 close $handle

				 return "Last added name for area deleted!"

				}

		"namezone" {

			    set LoadCoords 1
			    set Characters [string length $cargs]
			    set NameZone [string range $cargs 9 $Characters]

			    set handle [open "AntiPvP/namezone.txt" ]
			    gets $handle line
			    close $handle

			    set handle [open "AntiPvP/BackUp/namezone.bak" w+]
			    puts $handle $line
			    close $handle

			    set handle [open "AntiPvP/namezone.txt" w+]
			    puts $handle "$line{$NameZone} "
			    close $handle

			    return "PvP is forbidden in $NameZone!"

			   }

		"delzone" {

			   set handle [open "AntiPvP/numberofzones.txt" ]
			   gets $handle checkmax
			   close $handle

			   if { [ lindex $cargs 1 ] != "" && [ lindex $cargs 1 ] <= $checkmax } {

			   set handle [open "AntiPvP/numberofzones.txt" ]
			   gets $handle linenuz
			   close $handle

			   set handle [open "AntiPvP/numberofzones.txt" w+]
			   puts $handle [ expr ($linenuz - 1) ]
			   close $handle

			   set handle [open "AntiPvP/namezone.txt" ]
			   gets $handle linenaz
			   close $handle

			   set handle [open "AntiPvP/zone.txt" ]
			   gets $handle linez
			   close $handle

			   set clearedzone [ lindex $linenaz [ expr ([ lindex $cargs 1 ] - 1)] ]
			   set delentry [ expr ([ lindex $cargs 1 ] - 1)]

			   if { $delentry == 0 } {
			   
			   set handle [open "AntiPvP/namezone.txt" w+]
			   puts $handle ""
			   close $handle

			   set handle [open "AntiPvP/zone.txt" w+]
			   puts $handle ""
			   close $handle

			   }

			   for { set check 0 } { $check<$delentry } {incr check} {
			   set linezone [ lindex $linez $check ]
			   set linenamezone [ lindex $linenaz $check ]

			   if { $check > 0 } {

			   set handle [open "AntiPvP/namezone.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/namezone.txt" w+]
			   puts $handle "$write$linenamezone "
			   close $handle

			   set handle [open "AntiPvP/zone.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/zone.txt" w+]
			   puts $handle "$write$linezone "
			   close $handle

			   } else {

			   set handle [open "AntiPvP/zone.txt" w+]
			   puts $handle "$linezone "
			   close $handle

			   set handle [open "AntiPvP/namezone.txt" w+]
			   puts $handle "$linenamezone "
			   close $handle

			   }

			   }

			   for { set check [ lindex $cargs 1 ] } { $check<$linenuz } {incr check} {
			   set linezone [ lindex $linez $check ]
			   set linenamezone [ lindex $linenaz $check ]

			   if { $check > [ expr ([ lindex $cargs 1 ] - 1)] } {

			   set handle [open "AntiPvP/namezone.txt" ]
			   gets $handle writenz
			   close $handle

			   set handle [open "AntiPvP/zone.txt" ]
			   gets $handle writez
			   close $handle

			   } else {
			   set writenz ""
			   set writez ""
			   }

			   set handle [open "AntiPvP/namezone.txt" w+]
			   puts $handle "$writenz$linenamezone "
			   close $handle

			   set handle [open "AntiPvP/zone.txt" w+]
			   puts $handle "$writez$linezone "
			   close $handle

			   }

			   return "You deleted Zone ''$clearedzone''!"

			   } else {
			   return "Enter a valid number!"
			   }

			   }

		"resetnamezone" {

				 set LoadCoords 1

				 set handle [open "AntiPvP/BackUp/namezone.bak" ]
				 gets $handle line
				 close $handle

				 set handle [open "AntiPvP/namezone.txt" w+]
				 puts $handle $line
				 close $handle

				 return "Last added name for zone deleted!"

				}

		"addzone" {

			   set LoadCoords 1
			   set zone [ lindex $cargs 1 ]

			   set handle [open "AntiPvP/numberofzones.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/numberofzones.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/numberofzones.txt" w+]
			   puts $handle [ expr ($line + 1) ]
			   close $handle

			   set handle [open "AntiPvP/zone.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/zone.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/zone.txt" w+]
			   puts $handle "$line$zone "
			   close $handle

			   return "You added Zone: <[ lindex $cargs 1 ]>"

			  }

		"resetzone" {

			     set LoadCoords 1

			     set handle [open "AntiPvP/BackUp/numberofzones.bak" ]
			     gets $handle line
			     close $handle

			     set handle [open "AntiPvP/numberofzones.txt" w+]
			     puts $handle $line
			     close $handle

			     set handle [open "AntiPvP/BackUp/zone.bak" ]
			     gets $handle line
			     close $handle

			     set handle [open "AntiPvP/zone.txt" w+]
			     puts $handle $line
			     close $handle

			     return "Last added zone deleted!"

			    }

		"delarea" {

			   set handle [open "AntiPvP/numberofareas.txt" ]
			   gets $handle checkmax
			   close $handle

			   if { [ lindex $cargs 1 ] != "" && [ lindex $cargs 1 ] <= $checkmax } {

			   set handle [open "AntiPvP/numberofareas.txt" ]
			   gets $handle linenua
			   close $handle

			   set handle [open "AntiPvP/numberofareas.txt" w+]
			   puts $handle [ expr ($linenua - 1) ]
			   close $handle

			   set handle [open "AntiPvP/namearea.txt" ]
			   gets $handle linena
			   close $handle

			   set handle [open "AntiPvP/map.txt" ]
			   gets $handle linem
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" ]
			   gets $handle linexb
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" ]
			   gets $handle linexs
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" ]
			   gets $handle lineyb
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" ]
			   gets $handle lineys
			   close $handle

			   set clearedarea [ lindex $linena [ expr ([ lindex $cargs 1 ] - 1)] ]
			   set delentry [ expr ([ lindex $cargs 1 ] - 1)]

			   if { $delentry == 0 } {
			   
			   set handle [open "AntiPvP/namearea.txt" w+]
			   puts $handle ""
			   close $handle

			   set handle [open "AntiPvP/map.txt" w+]
			   puts $handle ""
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" w+]
			   puts $handle ""
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" w+]
			   puts $handle ""
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" w+]
			   puts $handle ""
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" w+]
			   puts $handle ""
			   close $handle

			   }

			   for { set check 0 } { $check<$delentry } {incr check} {
			   set linemap [ lindex $linem $check ]
			   set linenamearea [ lindex $linena $check ]
			   set linexbig [ lindex $linexb $check ]
			   set linexsmall [ lindex $linexs $check ]
			   set lineybig [ lindex $lineyb $check ]
			   set lineysmall [ lindex $lineys $check ]

			   if { $check > 0 } {

			   set handle [open "AntiPvP/namearea.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/namearea.txt" w+]
			   puts $handle "$write{$linenamearea} "
			   close $handle

			   set handle [open "AntiPvP/map.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/map.txt" w+]
			   puts $handle "$write$linemap "
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" w+]
			   puts $handle "$write$linexbig "
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" w+]
			   puts $handle "$write$linexsmall "
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" w+]
			   puts $handle "$write$lineybig "
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" ]
			   gets $handle write
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" w+]
			   puts $handle "$write$lineysmall "
			   close $handle

			   } else {

			   set handle [open "AntiPvP/map.txt" w+]
			   puts $handle "$linemap "
			   close $handle

			   set handle [open "AntiPvP/namearea.txt" w+]
			   puts $handle "{$linenamearea} "
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" w+]
			   puts $handle "$linexbig "
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" w+]
			   puts $handle "$linexsmall "
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" w+]
			   puts $handle "$lineybig "
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" w+]
			   puts $handle "$lineysmall "
			   close $handle

			   }

			   }

			   for { set check [ lindex $cargs 1 ] } { $check<$linenua } {incr check} {
			   set linemap [ lindex $linem $check ]
			   set linenamearea [ lindex $linena $check ]
			   set linexbig [ lindex $linexb $check ]
			   set linexsmall [ lindex $linexs $check ]
			   set lineybig [ lindex $lineyb $check ]
			   set lineysmall [ lindex $lineys $check ]

			   if { $check > [ expr ([ lindex $cargs 1 ] - 1)] } {

			   set handle [open "AntiPvP/namearea.txt" ]
			   gets $handle writena
			   close $handle

			   set handle [open "AntiPvP/map.txt" ]
			   gets $handle writem
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" ]
			   gets $handle writexb
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" ]
			   gets $handle writexs
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" ]
			   gets $handle writeyb
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" ]
			   gets $handle writeys
			   close $handle

			   } else {
			   set writena ""
			   set writem ""
			   set writexb ""
			   set writexs ""
			   set writeyb ""
			   set writeys ""
			   }

			   set handle [open "AntiPvP/xcoordsbig.txt" w+]
			   puts $handle "$writexb$linexbig "
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" w+]
			   puts $handle "$writexs$linexsmall "
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" w+]
			   puts $handle "$writeyb$lineybig "
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" w+]
			   puts $handle "$writeys$lineysmall "
			   close $handle

			   set handle [open "AntiPvP/namearea.txt" w+]
			   puts $handle "$writena{$linenamearea} "
			   close $handle

			   set handle [open "AntiPvP/map.txt" w+]
			   puts $handle "$writem$linemap "
			   close $handle

			   }

			   return "You deleted Area ''$clearedarea''!"

			   } else {
			   return "Enter a valid number!"
			   }

			   }


		"addarea" {

			   set LoadCoords 1
			   set Map [ lindex $cargs 1 ]
			   set XBig [ lindex $cargs 2 ]
			   set XSmall [ lindex $cargs 3 ]
			   set YBig [ lindex $cargs 4 ]
			   set YSmall [ lindex $cargs 5 ]

			   set handle [open "AntiPvP/numberofareas.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/numberofareas.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/numberofareas.txt" w+]
			   puts $handle [ expr ($line + 1) ]
			   close $handle

			   set handle [open "AntiPvP/map.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/map.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/map.txt" w+]
			   puts $handle "$line$Map "
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/xcoordsbig.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/xcoordsbig.txt" w+]
			   puts $handle "$line$XBig "
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/xcoordssmall.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/xcoordssmall.txt" w+]
			   puts $handle "$line$XSmall "
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/ycoordsbig.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/ycoordsbig.txt" w+]
			   puts $handle "$line$YBig "
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" ]
			   gets $handle line
			   close $handle

			   set handle [open "AntiPvP/BackUp/ycoordssmall.bak" w+]
			   puts $handle $line
			   close $handle

			   set handle [open "AntiPvP/ycoordssmall.txt" w+]
			   puts $handle "$line$YSmall "
			   close $handle

			   return "You have set the folling coordinates:\n<[ lindex $cargs 1 ]> <[ lindex $cargs 2 ]> <[ lindex $cargs 3 ]> <[ lindex $cargs 4 ]> <[ lindex $cargs 5 ]>\n\nMake sure that you add a name for the area now!"

			  }

		"resetarea" {

			  set LoadCoords 1

			  set rhandle [open "AntiPvP/BackUp/numberofareas.bak" ]
			  gets $rhandle line
			  close $rhandle

			  set handle [open "AntiPvP/numberofareas.txt" w+]
			  puts $handle $line
			  close $handle

			  set rhandle [open "AntiPvP/BackUp/map.bak" ]
			  gets $rhandle line
			  close $rhandle

			  set handle [open "AntiPvP/map.txt" w+]
			  puts $handle $line
			  close $handle

			  set rhandle [open "AntiPvP/BackUp/xcoordsbig.bak" ]
			  gets $rhandle line
			  close $rhandle

			  set handle [open "AntiPvP/xcoordsbig.txt" w+]
			  puts $handle $line
			  close $handle

			  set rhandle [open "AntiPvP/BackUp/xcoordssmall.bak" ]
			  gets $rhandle line
			  close $rhandle

			  set handle [open "AntiPvP/xcoordssmall.txt" w+]
			  puts $handle $line
			  close $handle

			  set rhandle [open "AntiPvP/BackUp/ycoordsbig.bak" ]
			  gets $rhandle line
			  close $rhandle

			  set handle [open "AntiPvP/ycoordsbig.txt" w+]
			  puts $handle $line
			  close $handle

			  set rhandle [open "AntiPvP/BackUp/ycoordssmall.bak" ]
			  gets $rhandle line
			  close $rhandle

			  set handle [open "AntiPvP/ycoordssmall.txt" w+]
			  puts $handle $line
			  close $handle

			  return "Last added area is reset!\nMake sure ot reset the name of the area if needed!"

			 }

		"zonecleanup" {
			       set handle [open "AntiPvP/namezone.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/numberofzones.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/zone.txt" w+]
			       puts $handle ""
			       close $handle
			       return "Zone Database cleaned!"
			      }

		"areacleanup" {
			       set handle [open "AntiPvP/map.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/namearea.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/numberofareas.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/xcoordsbig.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/xcoordssmall.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/ycoordsbig.txt" w+]
			       puts $handle ""
			       close $handle
			       set handle [open "AntiPvP/ycoordssmall.txt" w+]
			       puts $handle ""
			       close $handle
			       return "Area Database cleaned!"
			      }
	}
     }
}

}

::AntiPvP::init

Custom::AddCommand "antipvp" "AntiPvP::nAntiPvP"

  puts "\nAntiPvP by SOADfan and Team loaded...\nMay those rule breakers be punished :P\n"
